<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-2-25
 * Time: 下午8:57
 */

namespace backend\controllers;


use common\models\Menu;
use common\service\ConstantService;
use common\service\MenuService;
use common\service\LogaddService;

class MenuController extends BaseController
{

    /**
     * 列表页
     */
    public function actionIndex(){
        if($this->isGet() && $this->isAjax()){
            $menus = Menu::find()->asArray()->all();
            $data = MenuService::getCategory($menus);
            return $this->renderPageJson($data,count($data),ConstantService::$response_success);
        }
        return $this->render('index');
    }

    public function actionGetIndex(){
        if($this->isGet() && $this->isAjax()){
            $data = Menu::find()
                ->orderBy(['sort'=>SORT_ASC])
                ->asArray()->all();
            return $this->renderPageJson($data,count($data),ConstantService::$response_success);
        }
    }

    /**
     * 添加菜单
     */
    public function actionAddMenu(){
        $menu_model = new Menu();
        if($this->isPost() && $this->isAjax()){
            $data = $this->post();
            $row = Menu::find()->where(['menu_name'=>$data['menu_name']])->one();
            if(!empty($row)){
                return $this->renderJson([],'已经存在同名菜单',ConstantService::$response_fail);
            }
            if($data['parent_code']){
                $parent = Menu::find()->where(['menu_id'=>$data['parent_code']])->one();
                $data['level'] = $parent->level + 1;
            }else{
                $data['level'] = 1;
            }

            $menu_model->setAttributes($data);
            if($menu_model->save()){
                $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'新增菜单'.$data['menu_name'];
                LogaddService::logAdd($this->admin['username'],$str);
                return $this->renderJson([],'添加成功',ConstantService::$response_success);
            }else{
                $errors = $menu_model->getErrors();
                return $this->renderJson([],$errors,ConstantService::$response_fail);
            }
        }else{
            $arr = Menu::find()->asArray()->all();
            $menus = MenuService::getCategory($arr);
            return $this->render('add-menu',['menus'=>$menus]);
        }

    }

    /**
     * 删除菜单
     */
    public function actionDelMenu(){
        // 少用实例化对象,多用静态.
        $menu_model = new Menu();
        if($this->isAjax() && $this->post()){
            $menu_id = $this->post('menu_id');
            $mens = Menu::find()->where(['parent_code' => $menu_id])->asArray()->all();
            if(!empty($mens)){
                return $this->renderJson([],'有子集的分类不能直接删除',ConstantService::$response_fail);
            }
            $menu = $menu_model->find()->where(['menu_id'=>$menu_id])->one();
            $re = $menu->delete();
            if($re){
                $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'删除菜单'.$menu['menu_name'];
                LogaddService::logAdd($this->admin['username'],$str);
                return $this->renderJson([],'删除成功',ConstantService::$response_success);
            }else{
                return $this->renderJson([],'删除失败',ConstantService::$response_fail);
            }
        }
    }

    /**
     * 修改  用手动的自动验证方式少使用框架自定义验证方法
     */
    public function actionUpdateMenu(){
        //如果当菜单下有子集,不允许修改
        if($this->isAjax() && $this->isPost()){
            $data = $this->post();
            $menu_id = $this->post('menu_id');
            $row = Menu::find()->where(['menu_name'=>$data['menu_name']])->one();
            if(!empty($row) && $row['menu_id'] != $menu_id){
                return $this->renderJson([],'已经存在同名菜单',ConstantService::$response_fail);
            }
            $current_menu = Menu::find()->where(['menu_id'=>$menu_id])->one();
            $childs = Menu::find()->where(['parent_code'=>$menu_id])->asArray()->all();
            $menu_ids = array_column($childs,'menu_id');
            if(in_array($data['parent_code'],$menu_ids)){
                return $this->renderJson([],'不允许修改到自子节点下',ConstantService::$response_fail);
            }
            //获取层级
            if($data['parent_code']){
                $parent_menu = Menu::find()->where(['menu_id'=>$data['parent_code']])->one();
                $data['level'] = $parent_menu['level']+1;
                $child_level = $data['level']+1;
            }else{
                $data['level'] = 1;
                $child_level = 2;
            }

            $current_menu->setAttributes($data);
            if($current_menu->save()){
                if(!empty($menu_ids)){
                    $re = Menu::updateAll(['parent_code'=>$menu_id,'level'=>$child_level],['menu_id'=>$menu_ids]);
//                    if(!$re){
//                        $this->renderJson([],'修改失败请稍后再试2',ConstantService::$response_fail);
//                    }
                }
                $str = $this->admin['username'].'在'.date("Y-m-d H:i:s").'修改菜单'.$data['menu_name'];
                LogaddService::logAdd($this->admin['username'],$str);
                $this->renderJson([],'修改成功',ConstantService::$response_success);
            }else{

                $this->renderJson([],'修改失败,请稍后再试',ConstantService::$response_fail);
            }

        }else{
            $arr = Menu::find()->asArray()->all();
            $menus = MenuService::getCategory($arr);
            $menu_id = $this->get('id');
            $menu = Menu::find()->where(['menu_id'=>$menu_id])->asArray()->one();
            return $this->render('update-menu',['row'=>$menu,'menus'=>$menus]);
        }
    }



}